---
layout: "default"
title: "GeneratorType"
description: "Swift documentation for 'GeneratorType': Encapsulates iteration state and interface for iteration over a
sequence."
keywords: "GeneratorType,protocol,swift,documentation,next,Element"
root: "/v2.0"
---

<div class="intro-declaration"><code class="language-swift">protocol GeneratorType</code></div>

<div class="discussion comment">
    <p>Encapsulates iteration state and interface for iteration over a
<em>sequence</em>.</p>

<p><strong>Note:</strong> While it is safe to copy a <em>generator</em>, advancing one
  copy may invalidate the others.</p>

<p>Any code that uses multiple generators (or <code>for</code>...<code>in</code> loops)
over a single <em>sequence</em> should have static knowledge that the
specific <em>sequence</em> is multi-pass, either because its concrete
type is known or because it is constrained to <code>CollectionType</code>.
Also, the generators must be obtained by distinct calls to the
<em>sequence&#39;s</em> <code>generate()</code> method, rather than by copying.</p>
</div>

<table class="standard">
<tr>
<th id="inheritance">Inheritance</th>
<td>
<span class="viz"><a href="hierarchy/">View Protocol Hierarchy &rarr;</a></span>
</td>
</tr>

<tr>
<th id="aliases">Associated Types</th>
<td>
<span id="aliasesmark"></span>
<div class="declaration">
<code class="language-swift">Element</code>
<div class="comment">
    <p>The type of element generated by <code>self</code>.</p>
</div>
</div>
</td>
</tr>


<tr>
<th>Import</th>
<td><code class="language-swift">import Swift</code></td>
</tr>

</table>







<h3>Instance Methods</h3>
<div class="declaration" id="func-next">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-next">mutating func next()</a>
     <span class="required">Required</span>    
<div class="comment collapse" id="comment-func-next"><div class="p">
    <p>Advance to the next element and return it, or <code>nil</code> if no next
element exists.</p>

<p><strong>Requires:</strong> <code>next()</code> has not been applied to a copy of <code>self</code>
  since the copy was made, and no preceding call to <code>self.next()</code>
  has returned <code>nil</code>.  Specific implementations of this protocol
  are encouraged to respond to violations of this requirement by
  calling <code>preconditionFailure(&quot;...&quot;)</code>.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">mutating func next() -&gt; Self.Element?</code>
    
    
</div></div>
</div>


